#Boa:Dialog:comisiones

import wx
import wx.calendar
import wx.grid
import datetime

import consultasBD
import utiles
from decimal import Decimal

ROJO = (255, 145, 145, 255)
VERDE = (145, 255, 145, 255)
AZUL = (145, 145, 255, 255)
BLANCO = (255, 255, 255, 255)
AMARILLO = (254, 248, 191, 255)
GRIS = ( 220, 220, 220, 255)
NEGRO = ( 0, 0, 0, 255)
VERDENEGRO = ( 47, 79, 79, 255)

def create(parent):
    return comisiones(parent)

[wxID_COMISIONES, wxID_COMISIONESBUTTON1, wxID_COMISIONESBUTTON2, 
 wxID_COMISIONESBUTTON3, wxID_COMISIONESBUTTON4, wxID_COMISIONESCALENDARCTRL1, 
 wxID_COMISIONESCHECKBOX1, wxID_COMISIONESCOMBOBOX1, wxID_COMISIONESCOMBOBOX2, 
 wxID_COMISIONESDATEPICKERCTRL1, wxID_COMISIONESDATEPICKERCTRL2, 
 wxID_COMISIONESGRID1, wxID_COMISIONESSTATICBITMAP1, 
 wxID_COMISIONESSTATICBITMAP2, wxID_COMISIONESSTATICTEXT1, 
 wxID_COMISIONESSTATICTEXT10, wxID_COMISIONESSTATICTEXT11, 
 wxID_COMISIONESSTATICTEXT12, wxID_COMISIONESSTATICTEXT13, 
 wxID_COMISIONESSTATICTEXT14, wxID_COMISIONESSTATICTEXT15, 
 wxID_COMISIONESSTATICTEXT16, wxID_COMISIONESSTATICTEXT17, 
 wxID_COMISIONESSTATICTEXT18, wxID_COMISIONESSTATICTEXT19, 
 wxID_COMISIONESSTATICTEXT2, wxID_COMISIONESSTATICTEXT20, 
 wxID_COMISIONESSTATICTEXT21, wxID_COMISIONESSTATICTEXT22, 
 wxID_COMISIONESSTATICTEXT23, wxID_COMISIONESSTATICTEXT24, 
 wxID_COMISIONESSTATICTEXT25, wxID_COMISIONESSTATICTEXT3, 
 wxID_COMISIONESSTATICTEXT4, wxID_COMISIONESSTATICTEXT5, 
 wxID_COMISIONESSTATICTEXT6, wxID_COMISIONESSTATICTEXT7, 
 wxID_COMISIONESSTATICTEXT8, wxID_COMISIONESSTATICTEXT9, 
 wxID_COMISIONESTEXTCTRL1, wxID_COMISIONESTEXTCTRL10, 
 wxID_COMISIONESTEXTCTRL11, wxID_COMISIONESTEXTCTRL12, 
 wxID_COMISIONESTEXTCTRL13, wxID_COMISIONESTEXTCTRL14, 
 wxID_COMISIONESTEXTCTRL15, wxID_COMISIONESTEXTCTRL2, 
 wxID_COMISIONESTEXTCTRL3, wxID_COMISIONESTEXTCTRL4, wxID_COMISIONESTEXTCTRL5, 
 wxID_COMISIONESTEXTCTRL6, wxID_COMISIONESTEXTCTRL7, wxID_COMISIONESTEXTCTRL8, 
 wxID_COMISIONESTEXTCTRL9, 
] = [wx.NewId() for _init_ctrls in range(54)]

class comisiones(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_COMISIONES, name='comisiones',
              parent=prnt, pos=wx.Point(412, 88), size=wx.Size(660, 524),
              style=wx.DEFAULT_DIALOG_STYLE, title='Comisiones')
        self.SetClientSize(wx.Size(652, 497))

        self.grid1 = wx.grid.Grid(id=wxID_COMISIONESGRID1, name='grid1',
              parent=self, pos=wx.Point(16, 64), size=wx.Size(624, 208),
              style=0)

        self.staticText1 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT1,
              label='A\xf1o:', name='staticText1', parent=self, pos=wx.Point(16,
              8), size=wx.Size(24, 13), style=0)

        self.staticText2 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT2,
              label='Mes:', name='staticText2', parent=self, pos=wx.Point(104,
              8), size=wx.Size(24, 13), style=0)

        self.comboBox1 = wx.ComboBox(choices=[], id=wxID_COMISIONESCOMBOBOX1,
              name='comboBox1', parent=self, pos=wx.Point(40, 8),
              size=wx.Size(56, 21), style=0, value='')
        self.comboBox1.SetLabel('')

        self.comboBox2 = wx.ComboBox(choices=[], id=wxID_COMISIONESCOMBOBOX2,
              name='comboBox2', parent=self, pos=wx.Point(128, 8),
              size=wx.Size(130, 21), style=0, value='')
        self.comboBox2.SetLabel('')

        self.checkBox1 = wx.CheckBox(id=wxID_COMISIONESCHECKBOX1,
              label='por Periodo', name='checkBox1', parent=self,
              pos=wx.Point(16, 32), size=wx.Size(88, 13), style=0)
        self.checkBox1.SetValue(False)
        self.checkBox1.Bind(wx.EVT_CHECKBOX, self.OnCheckBox,
              id=wxID_COMISIONESCHECKBOX1)

        self.button1 = wx.Button(id=wxID_COMISIONESBUTTON1, label='Cargar',
              name='button1', parent=self, pos=wx.Point(294, 8),
              size=wx.Size(75, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnCargar,
              id=wxID_COMISIONESBUTTON1)

        self.button3 = wx.Button(id=wxID_COMISIONESBUTTON3, label='Salir',
              name='button3', parent=self, pos=wx.Point(566, 8),
              size=wx.Size(75, 23), style=0)
        self.button3.Bind(wx.EVT_BUTTON, self.OnSalir,
              id=wxID_COMISIONESBUTTON3)

        self.button2 = wx.Button(id=wxID_COMISIONESBUTTON2, label='Objetivos',
              name='button2', parent=self, pos=wx.Point(14, 456),
              size=wx.Size(75, 23), style=0)
        self.button2.Enable(False)
        self.button2.Bind(wx.EVT_BUTTON, self.OnObjetivos,
              id=wxID_COMISIONESBUTTON2)

        self.staticText3 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT3,
              label='Objetivo 1', name='staticText3', parent=self,
              pos=wx.Point(16, 280), size=wx.Size(50, 13), style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL1,
              name='textCtrl1', parent=self, pos=wx.Point(96, 280),
              size=wx.Size(80, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl1.SetEditable(False)

        self.staticText4 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT4,
              label='Objetivo 2', name='staticText4', parent=self,
              pos=wx.Point(16, 304), size=wx.Size(50, 13), style=0)

        self.textCtrl2 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL2,
              name='textCtrl2', parent=self, pos=wx.Point(96, 304),
              size=wx.Size(80, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl2.SetEditable(False)

        self.staticText5 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT5,
              label='Ventas del mes', name='staticText5', parent=self,
              pos=wx.Point(16, 328), size=wx.Size(73, 13), style=0)

        self.textCtrl3 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL3,
              name='textCtrl3', parent=self, pos=wx.Point(96, 328),
              size=wx.Size(80, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl3.SetEditable(False)

        self.textCtrl4 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL4,
              name='textCtrl4', parent=self, pos=wx.Point(176, 280),
              size=wx.Size(24, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl4.SetEditable(False)

        self.textCtrl5 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL5,
              name='textCtrl5', parent=self, pos=wx.Point(176, 304),
              size=wx.Size(24, 21), style=0, value='')
        self.textCtrl5.SetEditable(False)

        self.staticBitmap1 = wx.StaticBitmap(bitmap=wx.Bitmap(u'C:/erpsussan/imgs/pulgar-rojo.jpg',
              wx.BITMAP_TYPE_JPEG), id=wxID_COMISIONESSTATICBITMAP1,
              name='staticBitmap1', parent=self, pos=wx.Point(208, 280),
              size=wx.Size(64, 80), style=0)
        self.staticBitmap1.Show(False)

        self.staticBitmap2 = wx.StaticBitmap(bitmap=wx.Bitmap(u'C:/erpsussan/imgs/pulgar-verde.jpg',
              wx.BITMAP_TYPE_JPEG), id=wxID_COMISIONESSTATICBITMAP2,
              name='staticBitmap2', parent=self, pos=wx.Point(208, 280),
              size=wx.Size(64, 80), style=0)
        self.staticBitmap2.Show(False)

        self.datePickerCtrl1 = wx.DatePickerCtrl(id=wxID_COMISIONESDATEPICKERCTRL1,
              name='datePickerCtrl1', parent=self, pos=wx.Point(128, 32),
              size=wx.Size(80, 21), style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)
        self.datePickerCtrl1.Enable(False)

        self.datePickerCtrl2 = wx.DatePickerCtrl(id=wxID_COMISIONESDATEPICKERCTRL2,
              name='datePickerCtrl2', parent=self, pos=wx.Point(208, 32),
              size=wx.Size(80, 21), style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)
        self.datePickerCtrl2.Enable(False)

        self.textCtrl6 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL6,
              name='textCtrl6', parent=self, pos=wx.Point(464, 310),
              size=wx.Size(24, 21), style=0, value='')
        self.textCtrl6.SetEditable(False)

        self.textCtrl15 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL15,
              name='textCtrl15', parent=self, pos=wx.Point(504, 310),
              size=wx.Size(24, 21), style=0, value='')
        self.textCtrl15.Bind(wx.EVT_KILL_FOCUS, self.OnKillFeriado)

        self.textCtrl14 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL14,
              name='textCtrl14', parent=self, pos=wx.Point(552, 310),
              size=wx.Size(24, 21), style=0, value='')

        self.button4 = wx.Button(id=wxID_COMISIONESBUTTON4, label='Calcular',
              name='button4', parent=self, pos=wx.Point(584, 310),
              size=wx.Size(56, 23), style=0)
        self.button4.Bind(wx.EVT_BUTTON, self.OnCalcular,
              id=wxID_COMISIONESBUTTON4)

        self.staticText6 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT6,
              label='Promedio diario que deberia venderse', name='staticText6',
              parent=self, pos=wx.Point(16, 360), size=wx.Size(181, 13),
              style=0)

        self.textCtrl7 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL7,
              name='textCtrl7', parent=self, pos=wx.Point(96, 392),
              size=wx.Size(80, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl7.Enable(True)
        self.textCtrl7.SetEditable(False)

        self.textCtrl8 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL8,
              name='textCtrl8', parent=self, pos=wx.Point(96, 416),
              size=wx.Size(80, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl8.Enable(True)
        self.textCtrl8.SetEditable(False)

        self.staticText8 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT8,
              label='Promedio ventas', name='staticText8', parent=self,
              pos=wx.Point(464, 344), size=wx.Size(80, 13), style=0)

        self.textCtrl9 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL9,
              name='textCtrl9', parent=self, pos=wx.Point(576, 344),
              size=wx.Size(64, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl9.Enable(True)
        self.textCtrl9.SetEditable(False)

        self.textCtrl10 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL10,
              name='textCtrl10', parent=self, pos=wx.Point(464, 416),
              size=wx.Size(80, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl10.Enable(True)
        self.textCtrl10.SetEditable(False)

        self.staticText12 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT12,
              label='Debe vender diariamente', name='staticText12', parent=self,
              pos=wx.Point(328, 450), size=wx.Size(121, 13), style=0)

        self.staticText13 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT13,
              label='para alcanzar objetivos en', name='staticText13',
              parent=self, pos=wx.Point(328, 464), size=wx.Size(127, 13),
              style=0)

        self.textCtrl11 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL11,
              name='textCtrl11', parent=self, pos=wx.Point(560, 416),
              size=wx.Size(80, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl11.SetEditable(False)
        self.textCtrl11.Enable(True)

        self.textCtrl12 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL12,
              name='textCtrl12', parent=self, pos=wx.Point(464, 454),
              size=wx.Size(80, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl12.SetEditable(False)
        self.textCtrl12.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              'Tahoma'))

        self.staticText14 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT14,
              label='OBJETIVO 1', name='staticText14', parent=self,
              pos=wx.Point(464, 400), size=wx.Size(64, 13), style=0)
        self.staticText14.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, 'Tahoma'))

        self.staticText15 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT15,
              label='OBJETIVO 2', name='staticText15', parent=self,
              pos=wx.Point(560, 400), size=wx.Size(64, 13), style=0)
        self.staticText15.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, 'Tahoma'))

        self.staticText16 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT16,
              label='DIAS HABILES', name='staticText16', parent=self,
              pos=wx.Point(464, 278), size=wx.Size(78, 13), style=0)
        self.staticText16.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, 'Tahoma'))

        self.staticText10 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT10,
              label='Pendiente', name='staticText10', parent=self,
              pos=wx.Point(328, 416), size=wx.Size(48, 13), style=0)

        self.textCtrl13 = wx.TextCtrl(id=wxID_COMISIONESTEXTCTRL13,
              name='textCtrl13', parent=self, pos=wx.Point(560, 454),
              size=wx.Size(80, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl13.Enable(True)
        self.textCtrl13.SetEditable(False)
        self.textCtrl13.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              'Tahoma'))

        self.staticText11 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT11,
              label='-', name='staticText11', parent=self, pos=wx.Point(320,
              416), size=wx.Size(5, 13), style=0)
        self.staticText11.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, 'Tahoma'))

        self.staticText17 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT17,
              label='-', name='staticText17', parent=self, pos=wx.Point(318,
              450), size=wx.Size(5, 13), style=0)
        self.staticText17.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, 'Tahoma'))

        self.staticText18 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT18,
              label='totales', name='staticText18', parent=self,
              pos=wx.Point(464, 294), size=wx.Size(33, 13), style=0)

        self.staticText19 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT19,
              label='pendientes', name='staticText19', parent=self,
              pos=wx.Point(552, 294), size=wx.Size(53, 13), style=0)

        self.calendarCtrl1 = wx.calendar.CalendarCtrl(date=wx.DateTime.Now(),
              id=wxID_COMISIONESCALENDARCTRL1, name='calendarCtrl1',
              parent=self, pos=wx.Point(288, 280), size=wx.Size(165, 120),
              style=wx.calendar.CAL_SHOW_HOLIDAYS)
        self.calendarCtrl1.Bind(wx.EVT_SET_FOCUS, self.OnCambiarFocoCalendario)

        self.staticText7 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT7,
              label='el Objetivo 1', name='staticText7', parent=self,
              pos=wx.Point(16, 392), size=wx.Size(61, 13), style=0)

        self.staticText9 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT9,
              label='el Objetivo 2', name='staticText9', parent=self,
              pos=wx.Point(16, 408), size=wx.Size(61, 13), style=0)

        self.staticText20 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT20,
              label='(Objetivo-Ventas del mes)', name='staticText20',
              parent=self, pos=wx.Point(328, 430), size=wx.Size(125, 13),
              style=0)

        self.staticText21 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT21,
              label='durante los ... dias habiles para alcanzar',
              name='staticText21', parent=self, pos=wx.Point(16, 374),
              size=wx.Size(195, 13), style=0)

        self.staticText22 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT22,
              label='a la fecha actual', name='staticText22', parent=self,
              pos=wx.Point(464, 358), size=wx.Size(79, 13), style=0)

        self.staticText23 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT23,
              label='los .... dias que quedan.', name='staticText23',
              parent=self, pos=wx.Point(328, 478), size=wx.Size(118, 13),
              style=0)

        self.staticText24 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT24,
              label='staticText24', name='staticText24', parent=self,
              pos=wx.Point(384, 8), size=wx.Size(77, 14), style=0)
        self.staticText24.SetFont(wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, 'Tahoma'))

        self.staticText25 = wx.StaticText(id=wxID_COMISIONESSTATICTEXT25,
              label='feriados', name='staticText25', parent=self,
              pos=wx.Point(504, 294), size=wx.Size(39, 13), style=0)

    def __init__(self, parent):
        self.padre = parent
        self._init_ctrls(parent)
        nrosuc, nomsuc = utiles.datosSucursal()
        self.staticText24.SetLabel(nomsuc)
        if self.padre.loginRol=="root" or self.padre.loginRol=="contable":
            self.button2.Enable(True)
        self.cargarCombos()
        self.crearGrilla()
        feriados,diashabiles,pendientes = utiles.getTotalDias("domingo")
        self.textCtrl6.SetLabel(str(diashabiles-feriados))
        self.textCtrl14.SetLabel(str(pendientes))
        self.button1.SetFocus()
        
    def cargarCombos(self):
        anioinicial = 2008
        anioactual = int(utiles.obtenerFecha("Y-m-d")[:4])+1
        for year in range(anioinicial, anioactual):
            self.comboBox1.Append(str(year))
        self.comboBox1.SetSelection(len(self.comboBox1.GetItems())-1)
        self.comboBox2.Append("Enero")
        self.comboBox2.Append("Febrero")
        self.comboBox2.Append("Marzo")
        self.comboBox2.Append("Abril")
        self.comboBox2.Append("Mayo")
        self.comboBox2.Append("Junio")
        self.comboBox2.Append("Julio")
        self.comboBox2.Append("Agosto")
        self.comboBox2.Append("Septiembre")
        self.comboBox2.Append("Octubre")
        self.comboBox2.Append("Noviembre")
        self.comboBox2.Append("Diciembre")
        self.comboBox2.SetSelection(datetime.date.today().month-1)

    def limpiar(self):
        self.textCtrl1.SetLabel("0.00")
        self.textCtrl2.SetLabel("0.00")
        self.textCtrl3.SetLabel("0.00")
        self.textCtrl4.SetBackgroundColour(BLANCO)
        self.textCtrl4.SetLabel(" ")
        self.textCtrl5.SetBackgroundColour(BLANCO)
        self.textCtrl5.SetLabel(" ")
        if not self.checkBox1.GetValue():
            self.staticBitmap1.Show(False)
            self.staticBitmap1.Show(True)
        
    def OnCheckBox(self, event):
        if self.checkBox1.GetValue():
            self.comboBox1.Enable(False)
            self.comboBox2.Enable(False)
            self.datePickerCtrl1.Enable(True)
            self.datePickerCtrl2.Enable(True)
            self.textCtrl1.Show(False)
            self.textCtrl2.Show(False)
            self.textCtrl3.Show(False)
            self.textCtrl4.Show(False)
            self.textCtrl5.Show(False)
            self.staticText3.Show(False)
            self.staticText4.Show(False)
            self.staticText5.Show(False)
            self.staticBitmap1.Show(False)
            self.staticBitmap2.Show(False)
        else:
            self.comboBox1.Enable(True)
            self.comboBox2.Enable(True)
            self.datePickerCtrl1.Enable(False)
            self.datePickerCtrl2.Enable(False)
            self.textCtrl1.Show(True)
            self.textCtrl2.Show(True)
            self.textCtrl3.Show(True)
            self.textCtrl4.Show(True)
            self.textCtrl5.Show(True)
            self.staticText3.Show(True)
            self.staticText4.Show(True)
            self.staticText5.Show(True)
            self.staticBitmap1.Show(True)
            self.staticBitmap2.Show(True)

    def ajustarColumnas(self):
        for col in xrange(self.grid1.GetNumberCols()):
            self.grid1.AutoSizeColumn(col,True)
    
    def crearGrilla(self):
        #vendedora,en pesos,senias,tarjetas,ctacte,monto,comision,puntos,nc
        #0         1        2      3        4      5     6        7      8
        columnas =['Vendedora','En Pesos','Senia','Tarjetas\n/otros','Cta.Cte.',\
                   'Monto\nVendido','Comision','Puntos','N.C.']
        self.grid1.CreateGrid(1,len(columnas))
        for i, col in enumerate(columnas):
            self.grid1.SetColLabelValue(i, col)
        self.grid1.AutoSizeRow(0,True)
        self.ajustarColumnas()
        
    def limpiaGrilla(self):
        self.grid1.ClearGrid()
        for x in xrange(self.grid1.GetNumberRows()):
            self.grid1.DeleteRows()
        self.grid1.AppendRows()

    def OnSalir(self, event):
        self.Close()

    def OnCargar(self, event):
        self.limpiaGrilla()
        self.limpiar()
        sucursal = utiles.getSucursalActiva()
        
        if not self.checkBox1.GetValue():
            anio = self.comboBox1.GetLabel()
            mes = self.comboBox2.GetLabel()
            mes = str(self.comboBox2.GetSelection()+1)
            if len(mes)<2: mes = "0"+mes
            fechaInicio = "%s-%s-01 00:00:00" % (anio,mes)
            fechaFinal= "%s-%s-31 23:59:59" % (anio,mes)
        else:
            aux1 = self.datePickerCtrl1.GetLabel()
            aux2 = self.datePickerCtrl2.GetLabel()
            aux1 = aux1.replace("/","-")
            aux2 = aux2.replace("/","-")
            fechaInicio = utiles.invertirFecha(aux1)
            fechaFinal = utiles.invertirFecha(aux2)
            fechaInicio = fechaInicio+" 00:00:00"
            fechaFinal = fechaFinal+" 23:59:59"

        aux1 = fechaInicio[:10]
        aux2 = fechaFinal[:10]
##        r01 = "SELECT SUM( p1.importe ) AS total, p1.nombre "
##        r02 = "FROM ((SELECT v.totalfactura AS importe, vd.nombre "
##        r03 = "       FROM ventas v, vendedoras vd "
##        r04 = "       WHERE v.usuario = vd.idvendedora "
##        r05 = "             AND v.totalfactura >0 AND v.idformaticket!=999 "
##        r06 = "             AND fecha BETWEEN  '"+fechaInicio+"' AND '"+fechaFinal+"') "
##        r07 = "      UNION "
##        r08 = "      (SELECT vts.monto AS importe, vd.nombre "
##        r09 = "       FROM ventastienesenias vts, vendedoras vd "
##        r10 = "       WHERE vts.usuario = vd.idvendedora "
##        r11 = "             AND fecha BETWEEN  '"+aux1+"' AND '"+aux2+"'"
##        r12 = "             GROUP BY vts.idsenia)) AS p1 "
##        r13 = "WHERE 1 GROUP BY p1.nombre ORDER BY total DESC "
##        sql = r01+r02+r03+r04+r05+r06+r07+r08+r09+r10+r11+r12+r13

        r01 = "SELECT SUM( v.totalfactura ) , vd.nombre \n"
        r02 = "FROM ventas v, vendedoras vd \n"
        r03 = "WHERE v.fecha BETWEEN '"+fechaInicio+"' AND '"+fechaFinal+"' \n"
        r04 = "      AND v.totalfactura >0 AND vd.idvendedora = v.usuario \n"
        r05 = "GROUP BY vd.nombre ORDER BY SUM( v.totalfactura ) DESC \n"
        sql = r01+r02+r03+r04+r05

        lista = consultasBD.ordenCruda(sql)
        totalVendido = {}
        for item in lista:
            totalVendido[item[1]] = Decimal(str(item[0])).quantize(Decimal("0.10"))

        r1 = "SELECT vd.nombre,SUM(v.totalfactura) AS total \n"
        r2 = "FROM ventas v,vendedoras vd \n"
        r3 = "WHERE v.usuario=vd.idvendedora AND v.totalfactura<0 \n"
        r4 = "      AND fecha BETWEEN '"+fechaInicio+"' AND '"+fechaFinal+"' "
        r5 = "GROUP BY v.usuario ORDER BY total DESC "
        sql = r1+r2+r3+r4+r5

        resultado = consultasBD.ordenCruda(sql)
        totalNegativo = {}
        for item in resultado:
            totalNegativo[item[0]] = Decimal(str(item[1])).quantize(Decimal("0.10"))
        
        r01 = "SELECT p3.nombre,SUM(p3.monto) AS total \n"
        r02 = "FROM \n"
        r03 = "  (SELECT p1.idventa,p1.usuario,p2.nombre,\n"
        r04 = "          p1.totalfactura,p1.idformapagos,p1.monto \n"
        r05 = "   FROM \n"
        r06 = "     (SELECT v.idventa,v.usuario,v.totalfactura,vtp.idformapagos,\n"
        r07 = "             vtp.monto FROM ventas v, ventastienepagos vtp \n"
        r08 = "      WHERE v.fecha BETWEEN '"+fechaInicio+"' AND '"+fechaFinal+"' \n"
        r09 = "            AND v.idventa=vtp.idventa) AS p1, \n"
        r10 = "     vendedoras AS p2 \n"
        r11 = "  WHERE p1.usuario=p2.idvendedora) AS p3 \n"
        r12 = "WHERE p3.idformapagos='P' \n"
        r13 = "GROUP BY p3.nombre "
        sql = r01+r02+r03+r04+r05+r06+r07+r08+r09+r10+r11+r12+r13

        resultado = consultasBD.ordenCruda(sql)
        totalEfectivo = {}
        for item in resultado:
            totalEfectivo[item[0]]=Decimal(str(item[1])).quantize(Decimal("0.10"))

        aux1 = fechaInicio[:10]
        aux2 = fechaFinal[:10]
        r01 = "SELECT p3.nombre, SUM( p3.monto ) AS total \n"
        r02 = "FROM (SELECT p2.nombre, p1.monto \n"
        r03 = "      FROM (SELECT idsenia, usuario, monto \n"
        r04 = "            FROM ventastienesenias \n"
        r05 = "            WHERE fecha BETWEEN '"+aux1+"' AND '"+aux2+"' \n"
        r06 = "                   \n"
        r07 = "            GROUP BY idsenia) AS p1, vendedoras AS p2 \n"
        r08 = "      WHERE p1.usuario = p2.idvendedora) AS p3 \n"
        r09 = " WHERE 1 GROUP BY p3.nombre \n"
        sql = r01+r02+r03+r04+r05+r06+r07+r08+r09

        resultado = consultasBD.ordenCruda(sql)
        totalSenia = {}
        for item in resultado:
            totalSenia[item[0]]=Decimal(str(item[1])).quantize(Decimal("0.10"))
        
        r01 = "SELECT p3.nombre,SUM(p3.monto) AS total \n"
        r02 = "FROM \n"
        r03 = "  (SELECT p1.idventa,p1.usuario,p2.nombre,\n"
        r04 = "          p1.totalfactura,p1.idformapagos,p1.monto \n"
        r05 = "   FROM \n"
        r06 = "     (SELECT v.idventa,v.usuario,v.totalfactura,vtp.idformapagos,\n"
        r07 = "             vtp.monto FROM ventas v, ventastienepagos vtp \n"
        r08 = "      WHERE v.fecha BETWEEN '"+fechaInicio+"' AND '"+fechaFinal+"' \n"
        r09 = "            AND v.idventa=vtp.idventa) AS p1, \n"
        r10 = "     vendedoras AS p2 \n"
        r11 = "  WHERE p1.usuario=p2.idvendedora) AS p3 \n"
        r12 = "WHERE p3.idformapagos<>'P' \n"
        r13 = "GROUP BY p3.nombre "
        sql = r01+r02+r03+r04+r05+r06+r07+r08+r09+r10+r11+r12+r13

        resultado = consultasBD.ordenCruda(sql)
        totalOtros = {}
        for item in resultado:
            totalOtros[item[0]]=Decimal(str(item[1])).quantize(Decimal("0.10"))

        sql = "SELECT nombre FROM vendedoras WHERE sucursal='"+sucursal+"' "
        vendedoras = consultasBD.ordenCruda(sql)
        tot1 = Decimal("0.00") #total en efectivo
        tot2 = Decimal("0.00") #total tarjeta
        tot3 = Decimal("0.00") #total cta cte
        tot4 = Decimal("0.00") #total vendido
        tot5 = Decimal("0.00") #total comision
        tot6 = Decimal("0.00") #total Nta Credito
        tot7 = Decimal("0.00") #total senia
        for fil,usuario in enumerate(lista):
            vendedora = usuario[1]
            try:
                vendido = totalVendido[vendedora]
                tot4+=vendido
            except:
                vendido = Decimal("0.00")
            try:
                efectivo = totalEfectivo[vendedora]
                tot1+=efectivo
            except:
                efectivo = Decimal("0.00")
            try:
                senia = totalSenia[vendedora]
                tot7+=senia
            except:
                senia = Decimal("0.00")
            try:
                otros = totalOtros[vendedora]
                tot2+=otros
            except:
                otros = Decimal("0.00")
            try:
                negativo = totalNegativo[vendedora]
                tot6+=negativo
            except:
                negativo = Decimal("0.00")
            ctacte = vendido-efectivo-otros-senia
            tot3+=ctacte
            comision = (vendido*Decimal("0.01")).quantize(Decimal("0.10"))
            tot5+=comision

            #vendedora,en pesos,senias,tarjetas,ctacte,monto,comision,puntos,nc
            #0         1        2      3        4      5     6        7      8
            self.grid1.SetCellValue(fil,0,vendedora)
            self.grid1.SetCellValue(fil,1,str(efectivo))
            self.grid1.SetCellValue(fil,2,str(senia))
            self.grid1.SetCellValue(fil,3,str(otros))
            self.grid1.SetCellValue(fil,4,str(ctacte))
            self.grid1.SetCellValue(fil,5,str(vendido))
            self.grid1.SetCellValue(fil,6,str(comision))
            self.grid1.SetCellValue(fil,8,str(negativo))
            
            self.grid1.SetCellBackgroundColour(fil,5,GRIS)
            self.grid1.SetCellAlignment(fil, 1, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 2, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 3, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 4, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 5, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 6, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 8, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.AppendRows()

        if self.grid1.GetNumberRows()<2:
            self.textCtrl4.SetBackgroundColour(BLANCO)
            self.textCtrl5.SetBackgroundColour(BLANCO)
            self.textCtrl4.SetLabel(" ")
            self.textCtrl5.SetLabel(" ")
            self.staticBitmap1.Show(False)
            self.staticBitmap2.Show(False)
            self.staticBitmap1.Refresh()
            self.staticBitmap2.Refresh()
            return
        #vendedora,en pesos,senias,tarjetas,ctacte,monto,comision,puntos,nc
        #0         1        2      3        4      5     6        7      8
        fil+=1
        self.grid1.SetCellValue(fil,0,"TOTALES")
        self.grid1.SetCellValue(fil,1,str(tot1))
        self.grid1.SetCellValue(fil,2,str(tot7))
        self.grid1.SetCellValue(fil,3,str(tot2))
        self.grid1.SetCellValue(fil,4,str(tot3))
        self.grid1.SetCellValue(fil,5,str(tot4))
        self.grid1.SetCellValue(fil,6,str(tot5))
        self.grid1.SetCellValue(fil,8,str(tot6))

        self.grid1.SetCellBackgroundColour(fil,5,GRIS)
        self.grid1.SetCellAlignment(fil, 1, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellAlignment(fil, 2, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellAlignment(fil, 3, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellAlignment(fil, 4, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellAlignment(fil, 5, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellAlignment(fil, 6, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellAlignment(fil, 8, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)

        self.ajustarColumnas()

        if self.checkBox1.GetValue(): return

        sucursal = str(utiles.getSucursalActiva())
        anio = self.comboBox1.GetLabel()
        mes = str(self.comboBox2.GetSelection()+1)
        if len(mes)<2: mes = "0"+mes

        fecha = anio+mes
        r1 = "SELECT objetivo1,objetivo2 FROM objetivos "
        r2 = "WHERE fecha='"+fecha+"' AND sucursal='"+sucursal+"' "
        sql = r1+r2
        resultado = consultasBD.ordenCrudaDict(sql)
        if resultado==() or resultado==None or len(resultado)==0:
            objetivo1 = Decimal("0.00")
            objetivo2 = Decimal("0.00")
        else:
            objetivo1 = resultado[0]["objetivo1"]
            objetivo2 = resultado[0]["objetivo2"]
            
        #vendido = tot4+tot6
        vendido = tot4
        
        self.textCtrl1.SetLabel(str(objetivo1))
        self.textCtrl2.SetLabel(str(objetivo2))
        self.textCtrl3.SetLabel(str(vendido))

        valor = Decimal(self.textCtrl3.GetLabel()).quantize(Decimal("0.00"))

        if valor==Decimal("0.00"):
            self.textCtrl4.SetBackgroundColour(BLANCO)
            self.textCtrl5.SetBackgroundColour(BLANCO)
            self.textCtrl4.SetLabel(" ")
            self.textCtrl5.SetLabel(" ")
            self.staticBitmap1.Show(False)
            self.staticBitmap2.Show(False)
            self.staticBitmap1.Refresh()
            self.staticBitmap2.Refresh()
            return

        if vendido<objetivo1:
            self.textCtrl4.SetBackgroundColour(ROJO)
            self.textCtrl4.SetLabel(" ")
        else:
            self.textCtrl4.SetBackgroundColour(VERDE)
            self.textCtrl4.SetLabel(" ")

        if vendido<objetivo2:
            self.textCtrl5.SetBackgroundColour(ROJO)
            self.textCtrl5.SetLabel(" ")
            self.staticBitmap1.Show(True)
            self.staticBitmap2.Show(False)
        else:
            self.textCtrl5.SetBackgroundColour(VERDE)
            self.textCtrl5.SetLabel(" ")
            self.staticBitmap1.Show(False)
            self.staticBitmap2.Show(True)

    def OnObjetivos(self, event):
        import Comisiones3objetivos
        dlg = Comisiones3objetivos.Dialog1(self)
        try:
            dlg.ShowModal()
        finally:
            dlg.Destroy()
        self.OnCargar(None)

    def OnCambiarFocoCalendario(self, event):
        self.textCtrl6.SetFocus()

    def OnCalcular(self, event):
        try:
            dias = int(self.textCtrl6.GetLabel())
            if dias == 0 : dias = 1
        except:
            utiles.mensaje(None,"Debe indicar un valor numerico\npara el calculo")
            self.textCtrl6.SetFocus()
            return
        try:
            pendientes = int(self.textCtrl14.GetLabel())
            if pendientes == 0: pendientes = 1
        except:
            utiles.mensaje(None,"Debe indicar un valor numerico\npara el calculo")
            self.textCtrl14.SetFocus()
            return
        
        texto = "los "+self.textCtrl14.GetLabel()+" dias que quedan."
        self.staticText23.SetLabel(texto)
        texto = "durante los "+self.textCtrl6.GetLabel()+" dias habiles para alcanzar"
        self.staticText21.SetLabel(texto)
        
        montoObjetivo1 = Decimal(self.textCtrl1.GetLabel())
        montoObjetivo2 = Decimal(self.textCtrl2.GetLabel())
        montoVendido   = Decimal(self.textCtrl3.GetLabel())

        ventaPorDiaOficialSegunOb1 = str(montoObjetivo1 / dias)
        ventaPorDiaOficialSegunOb1 = Decimal(ventaPorDiaOficialSegunOb1).quantize(Decimal("0.10"))
        ventaPorDiaOficialSegunOb2 = str(montoObjetivo2 / dias)
        ventaPorDiaOficialSegunOb2 = Decimal(ventaPorDiaOficialSegunOb2).quantize(Decimal("0.10"))

        if dias==pendientes:
            aux = 1
        else:
            aux = dias=pendientes

        ventaRealDiariaPromedio  = str(montoVendido / (aux))
        ventaRealDiariaPromedio  = Decimal(ventaRealDiariaPromedio).quantize(Decimal("0.10"))

        pendienteObj1 = montoObjetivo1 - montoVendido
        pendienteObj2 = montoObjetivo2 - montoVendido

        porVenderOb1 = str(pendienteObj1 / pendientes)
        porVenderOb1 = Decimal(porVenderOb1).quantize(Decimal("0.10"))

        porVenderOb2 = str(pendienteObj2 / pendientes)
        porVenderOb2 = Decimal(porVenderOb2).quantize(Decimal("0.10"))
        
        self.textCtrl9.SetLabel(str(ventaRealDiariaPromedio))
        self.textCtrl7.SetLabel(str(ventaPorDiaOficialSegunOb1))
        self.textCtrl8.SetLabel(str(ventaPorDiaOficialSegunOb2))
        self.textCtrl10.SetLabel(str(pendienteObj1))
        self.textCtrl11.SetLabel(str(pendienteObj2))
        self.textCtrl12.SetLabel(str(porVenderOb1))
        self.textCtrl13.SetLabel(str(porVenderOb2))
        self.textCtrl6.SetFocus()

    def OnKillFeriado(self, event):
        try:
            valor = int(self.textCtrl15.GetLabel())
        except:
            valor = 0
        totaldias = int(self.textCtrl6.GetLabel())
        totaldiashabiles = totaldias-valor
        self.textCtrl6.SetLabel(str(totaldiashabiles))
